import cv2
import numpy as np
import matplotlib.pyplot as plt

img = cv2.imread("./img/cat.png")
gray_img = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)

# 颜色值范围 0 ~ 255
x = np.arange(0,256)
# 设置一个同x一样长度的矩阵
y = np.zeros(x.shape)

# 遍历颜色范围值
h,w = gray_img.shape
for i in range(h):
    for j in range(w):
        for k in x:
            if gray_img[i][j] == k:
                y[k] = y[k] + gray_img[i][j] / 255

# 绘制整个像素点统计直方图
plt.plot(x,y,'r-')
plt.show()
